﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>生产派单</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="~/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
</head>
<body>
    <form class="layui-form" action="" id="dispatchForm">
        <div class="layui-form-item">
            <label class="layui-form-label">生产工单编号</label>
            <div class="layui-input-block">
                @if (!string.IsNullOrEmpty(ViewBag.ParentWorkOrderCode))
                {
                    <input type="text" name="ProductionWorkOrderCode" value="@ViewBag.ParentWorkOrderCode" readonly lay-verify="required" placeholder="请输入生产工单编号" autocomplete="off" class="layui-input layui-disabled">
                    <div class="layui-form-mid layui-word-aux">继承自父级工单编号</div>
                }
                else
                {
                    <input type="text" name="ProductionWorkOrderCode" lay-verify="required" placeholder="请输入生产工单编号" autocomplete="off" class="layui-input">
                }
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">工单名称</label>
            <div class="layui-input-block">
                <input type="text" name="WorkOrderName" lay-verify="required" placeholder="请输入工单名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">任务名称</label>
            <div class="layui-input-block">
                <input type="text" name="TaskName" lay-verify="required" placeholder="请输入任务名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">工作站名称</label>
            <div class="layui-input-block">
                <input type="text" name="WorkGroupName" lay-verify="required" placeholder="请输入工作站名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">工序名称</label>
            <div class="layui-input-block">
                <select name="DocumentStatus" lay-verify="required">
                    <option value="">请选择工序名称</option>
                    <option value="1">印刷</option>
                    <option value="2">复合</option>
                    <option value="3">分切</option>
                    <option value="4">制袋</option>
                </select>
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">产品物料名称</label>
            <div class="layui-input-block">
                <input type="text" name="ProductMaterialName" lay-verify="required" placeholder="请输入产品物料名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">开始生产日期</label>
                <div class="layui-input-inline layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-date"></i>
                    </div>
                    <input type="text" name="ProductionStartTime" id="startDate" lay-verify="required" placeholder="yyyy-MM-dd HH:mm:ss" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">完成生产日期</label>
                <div class="layui-input-inline layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-date"></i>
                    </div>
                    <input type="text" name="ProductionEndTime" id="endDate" lay-verify="required" placeholder="yyyy-MM-dd HH:mm:ss" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">派单对象</label>
            <div class="layui-input-block">
                <input type="text" name="OrderTarget" lay-verify="required" placeholder="请输入派单对象" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
    <script src="~/scripts/jquery-3.4.1.min.js"></script>
    <script src="~/scripts/layui-v2.5.6/layui/layui.js"></script>
    <script>
        layui.use(['form', 'laydate', 'layer'], function(){
            var form = layui.form;
            var laydate = layui.laydate;
            var layer = layui.layer;
            
            // 页面加载时自动获取工单编号，如果不是子级添加的话
            @if (string.IsNullOrEmpty(ViewBag.ParentWorkOrderCode))
            {
                <text>loadWorkOrderCode();</text>
            }
            
            // 执行一个laydate实例 - 开始时间
            laydate.render({
                elem: '#startDate',
                type: 'datetime',
                format: 'yyyy-MM-dd HH:mm:ss'
            });
            
            // 执行一个laydate实例 - 结束时间
            laydate.render({
                elem: '#endDate',
                type: 'datetime',
                format: 'yyyy-MM-dd HH:mm:ss'
            });
            
            // 获取工单编号
            function loadWorkOrderCode() {
                // 使用时间戳确保每次刷新都有不同的编号
                var now = new Date();
                var dateStr = now.getFullYear() + 
                             (now.getMonth() + 1).toString().padStart(2, '0') + 
                             now.getDate().toString().padStart(2, '0');
                var timeStr = now.getHours().toString().padStart(2, '0') + 
                             now.getMinutes().toString().padStart(2, '0') + 
                             now.getSeconds().toString().padStart(2, '0');
                var defaultCode = 'MO' + dateStr + timeStr;
                $('input[name="ProductionWorkOrderCode"]').val(defaultCode);
                
                // 备用方案：如果需要调用后端接口
                /*
                $.ajax({
                    url: '/ProductionDispatchMain/GetNextWorkOrderCode',
                    type: 'GET',
                    dataType: 'json',
                    success: function(response) {
                        $('input[name="ProductionWorkOrderCode"]').val(response);
                    },
                    error: function() {
                        console.log('获取工单编号失败，使用本地生成');
                        $('input[name="ProductionWorkOrderCode"]').val(defaultCode);
                    }
                });
                */
            }
            
            // 监听提交
            form.on('submit(formDemo)', function(data){
                // 显示加载层
                var loadIndex = layer.load(2, {
                    shade: [0.3, '#000']
                });
                
                // 准备提交数据
                var formData = data.field;
                
                // 发送AJAX请求
                $.ajax({
                    url: '/ProductionDispatchMain/AddProductionDispatch',
                    type: 'POST',
                    data: formData,
                    success: function(response) {
                        layer.close(loadIndex);
                        if (response === 1) {
                            layer.msg('生产派单添加成功！', {
                                icon: 1,
                                time: 1500
                            }, function(){
                                @if (!string.IsNullOrEmpty(ViewBag.ParentWorkOrderCode))
                                {
                                    <text>
                                    // 如果是添加子级，关闭弹窗并返回显示页面
                                    var index = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(index);
                                    parent.location.href = '/ProductionDispatchMain/GetDispatch';
                                    </text>
                                }
                                else
                                {
                                    <text>
                                    // 成功后重置表单
                                    document.getElementById('dispatchForm').reset();
                                    form.render(); // 重新渲染表单
                                    // 重新获取下一个工单编号
                                    loadWorkOrderCode();
                                    </text>
                                }
                            });
                        } else {
                            layer.msg('添加失败，请重试！', {
                                icon: 2,
                                time: 2000
                            });
                        }
                    },
                    error: function(xhr, status, error) {
                        layer.close(loadIndex);
                        layer.msg('提交失败：' + error, {
                            icon: 2,
                            time: 3000
                        });
                    }
                });
                
                return false; // 阻止表单默认提交
            });
        });
    </script>

</body>
</html>